package se.ytterman.jserial.examples.gsm;

import java.lang.*;
import java.io.*;

import org.w3c.dom.*;

import se.ytterman.jserial.framework.device.*;

import se.ytterman.jserial.util.*;

// Classes used for JNI local port usage
import se.ytterman.jserial.jni.SerialInterfaceFactory;
import se.ytterman.jserial.jni.SerialInterface;

// Classes used for TCP port simulation
import se.ytterman.jserial.net.TCPServerInterface;

import se.ytterman.jserial.io.CommunicationAdapter;

public class GSMPhoneExample {

    public static int step_delay = 1500;
    
    public static void main(String argv[]) {
        // Create GSM phone model reference
        GSMPhone phone = null;    
               
        // Check if we have any runtime parameters
        if(argv.length > 0) {   
            // Example using local physical port
            if(argv[0].compareTo("local") == 0) {
                System.out.println("Running GSM example on physical port...");
                
                try {               
                    // Create serial interface factory 
                    SerialInterfaceFactory factory = new SerialInterfaceFactory();
		
                    // Fetch interface array
                    SerialInterface interfaces[] = factory.getInterfaceArray();
        
                    // XXX, OK this is /dev/ttyS0 on Linux...
                    CommunicationAdapter commAdapter = interfaces[0];

                    // Enable delays
                    GSMPhoneExample.step_delay = 1600;
                    
                    // Create phone
                    phone = new GSMPhone(commAdapter, argv[1], argv[2]);
                }
                catch(Exception e) {
                    System.out.println("Local: " + e.getMessage());
                    e.printStackTrace();
                }
            }            
            
            // Example using simulation, listening TCP server
            if(argv[0].compareTo("tcp") == 0) {
                System.out.println("Running GSM example on simulated port...");
                
                try {
                    // Create simple TCP listener (telnet) server and wait for client    
                    CommunicationAdapter commAdapter = new TCPServerInterface(23);
                    
                    // Enable delays
                    GSMPhoneExample.step_delay = 1;
                    
                    // Create phone object
                    phone = new GSMPhone(commAdapter, argv[1], argv[2]);
                }
                catch(Exception e) {
                    System.out.println("TcpServer: " + e.getMessage());
                }
            }
            
            // Example using simulation, listening TCP server
            if(argv[0].compareTo("test") == 0) {
                System.out.println("Debug stub for GSM phone model development");
                
                try {
                    // Create simple dummy communication adapter    
                    CommunicationAdapter commAdapter = new DummyServerInterface();
                              
                    // Disable delays
                    GSMPhoneExample.step_delay = 1;
                    
                    // Create phone object
                    phone = new GSMPhone(commAdapter, argv[1], argv[2]);
                }
                catch(Exception e) {
                    System.out.println("Test: " + e.getMessage());
                }
            }
            
            // Run example...
            if(false) {
                // Set reference to the application API
                GSMPhoneExample.phoneApi = phone.getGSMPhoneApi();
                
                try {
                    GSMPhoneExample.phoneApi.registerPhone("0696");
                    GSMPhoneExample.phoneApi.setShortMessageParameters("+46708000708");
                    GSMPhoneExample.phoneApi.sendShortMessage("+46708124245", "Test!");
                }
                catch(Exception e) {
                    System.out.println(e.getMessage());
                }
            }
        }
        else {
            System.out.println("Not enough parameters.");            
        }
    }

    private static GSMPhoneApi phoneApi;
}
